import requests
from lxml import etree
import json
import time
import threading

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                         "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"
}
news_set = set()


def get_data():
    url = "https://news.163.com/special/epidemic/"
    html = requests.get(url, headers=headers)
    soup = etree.HTML(html.text)
    current_time = soup.xpath('//div[@class="cover_time"]/text()')[0]
    cover_data = soup.xpath('//div[@class="cover_data"]/div[starts-with(@class,"cover")]')
    while True:
        for cover in cover_data:
            title = cover.xpath("h4/text()")[0]
            number = cover.xpath('div[@class="number"]/text()')[0]
            result = current_time + ' ' + title + ' ' + number
            if result not in news_set:
                news_set.add(result)
                print(title, number, end=" ")

    time.sleep(60 * 1)


def get_news():
    url = "https://opendata.baidu.com/data/inner?tn=reserved_all_res_tn&dspName=iphone&from_sf=1&dsp=iphone&resource_id=28565&alr=1&query=%E8%82%BA%E7%82%8E&cb=jsonp_1581049040543_70430"
    html = requests.get(url, headers=headers)
    html_text = html.text
    start = html_text.find('{"ResultCode"')
    end = html_text.find(r'srcids\u0000\u0000"}') + len(r'srcids\u0000\u0000"}')
    json_data = json.loads(html_text[start:end])
    data_news = json_data['Result'][0]['DisplayData']['result']['items']
    while True:
        for data in data_news:
            news_title = data['eventDescription']
            news_time = data['eventTime']
            local_time = time.localtime(int(news_time))
            current_time = time.strftime('%Y-%m-%d %H-%M-%S', local_time)
            result = news_title + current_time
            if result not in news_set:
                news_set.add(result)
                print(result)

    time.sleep(60 * 1)


def main():
    print("网易新闻：")
    # get_data()
    threading.Thread(target=get_data).start()
    print("百度新闻：")
    # get_news()
    threading.Thread(target=get_news).start()


if __name__ == '__main__':
    main()
